import pandas as pd
import matplotlib.pyplot as plt

def emotion_pie():
    # 读取Excel文件
    file_path = '../public/data/tables/data_emotion_analysis.xlsx'  # 替换为你的Excel文件路径
    df = pd.read_excel(file_path)

    # 假设Excel文件中有 'pos', 'neg', 'mid' 三列
    data = df[['pos', 'neg', 'mid']]

    # 分类计数
    positive_count = 0
    neutral_count = 0
    negative_count = 0

    for index, row in data.iterrows():
        pos = row['pos']
        neg = row['neg']
        mid = row['mid']

        if neg > 0.6:
            negative_count += 1
        elif pos > 0.6:
            positive_count += 1
        else:
            neutral_count += 1

    # 绘制饼图
    labels = ['Positive', 'Neutral', 'Negative']
    sizes = [positive_count, neutral_count, negative_count]
    colors = ['#ff9999', '#66b3ff', '#99ff99']
    explode = (0.1, 0, 0)  # 仅仅 "爆炸" 第一个切片 (即 'Positive')

    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%',
            shadow=True, startangle=90)
    ax1.axis('equal')  # 确保饼图是一个正圆

    plt.title('Attitude Distribution')
    plt.savefig('../public/data/img/emotion_pie.png')
    # plt.show()

if __name__ == '__main__':
    emotion_pie()